package com.dora.rabbitmq.controller;

import com.dora.rabbitmq.po.Mail;
import com.dora.rabbitmq.po.TopicMail;
import com.dora.rabbitmq.service.impl.ProducerImpl;
import com.dora.rabbitmq.service.impl.PublisherImpl;
import com.dora.redis.domian.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * RabbitMQ 控制器
 *
 * @author Clarence
 * @date 2024/03/03
 */
@Controller
public class RabbitMQController {


    @Autowired
    ProducerImpl producer;

    @Autowired
    PublisherImpl publisher;

    @RequestMapping(value = "/produce", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Result produce(@ModelAttribute("mail") Mail mail) throws Exception {
        producer.sendMail("myqueue", mail);
        return Result.ok();
    }

    @RequestMapping(value = "/topic", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Result topic(@ModelAttribute("mail") Mail mail) throws Exception {
        publisher.publishMail(mail);
        return Result.ok();
    }

    @RequestMapping(value = "/direct", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Result direct(@ModelAttribute("mail") TopicMail mail) {
        Mail m = new Mail(mail.getMailId(), mail.getCountry(), mail.getWeight());
        publisher.sendDirectMail(m, mail.getRoutingKey());
        return Result.ok();
    }

    @RequestMapping(value = "/mytopic", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Result topic(@ModelAttribute("mail") TopicMail mail) {
        Mail m = new Mail(mail.getMailId(), mail.getCountry(), mail.getWeight());
        publisher.sendTopicMail(m, mail.getRoutingKey());
        return Result.ok();
    }

    @RequestMapping("demo")
    public String demo() {
        return "demo";
    }

}
